using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
/*
	Remove SQL server specific code prior to upgrade.
	Create new property AllowChanges
	*** Refactor class name from connInfo (urgh) to ConnectionInfo

	2013 Adding a propert for encripting
*/
namespace DbView
{
	public class ConnectionInfo
	{
		public String Name = "";
		public String Driver = "";
		public String Server = "";
		public String Database = "";
		public String UID = "";
		public String PWD = "";
		public bool Encrpyted = false;
		public bool AllowChanges = false;
		bool isDeleted = false;

		XmlNode node;

		public ConnectionInfo()
		{
		}

		// framework from "Guidelines for Overloading Equals() and Operator == (C# Programming Guide)"
		public static bool operator ==(ConnectionInfo a, ConnectionInfo b)
		{
			// If both are null, or both are same instance, return true.
			if (System.Object.ReferenceEquals(a, b))
			{
				return true;
			}

			// If one is null, but not both, return false.
			if (((object)a == null) || ((object)b == null))
			{
				return false;
			}

			// Return true if the fields match:
			return a.Name == b.Name && 
					a.Server == b.Server && 
					a.Database == b.Database &&
					a.UID == b.UID &&
					a.isDeleted == b.isDeleted;
		}

		public static bool operator !=(ConnectionInfo a, ConnectionInfo b)
		{
			return !(a == b);
		}

		public ConnectionInfo(XmlNode node)
		{
			this.node = node;
			this.Name = node.Attributes[DbView.Resources.ConfigDoc.CFGATT_NAME].Value;
			this.Driver = node.Attributes[DbView.Resources.ConfigDoc.CFGATT_DRIVER].Value;
			this.Server = node.Attributes[DbView.Resources.ConfigDoc.CFGATT_SERVER].Value;
			this.Database = node.Attributes[DbView.Resources.ConfigDoc.CFGATT_DATABASE].Value;
			this.UID = node.Attributes[DbView.Resources.ConfigDoc.CFGATT_USER].Value;
			this.PWD = node.Attributes[DbView.Resources.ConfigDoc.CFGATT_PASSWORD].Value;
			// definitely false as a default. Old connections will not have these values set.
			this.AllowChanges = DbView.Resources.ConfigDoc.Instance.AttributeValue(node, DbView.Resources.ConfigDoc.CFGATT_DEV, false);
			this.Encrpyted = DbView.Resources.ConfigDoc.Instance.AttributeValue(node, DbView.Resources.ConfigDoc.CFGATT_ENCRYTED, false);
		}

		public void RefreshNode()
		{
			if (!this.isDeleted)
			{
				if (this.node == null)
					this.node = DbView.Resources.ConfigDoc.Instance.CreateNewRecordInGroup(DbView.Resources.ConfigDoc.CFGELM_CONNECTIONS);
				// remove and add back
				this.node.RemoveAll();
				DbView.Resources.ConfigDoc.Instance.AddAttribute(this.node, DbView.Resources.ConfigDoc.CFGATT_NAME, this.Name);
				DbView.Resources.ConfigDoc.Instance.AddAttribute(this.node, DbView.Resources.ConfigDoc.CFGATT_DRIVER, this.Driver);
				DbView.Resources.ConfigDoc.Instance.AddAttribute(this.node, DbView.Resources.ConfigDoc.CFGATT_SERVER, this.Server);
				DbView.Resources.ConfigDoc.Instance.AddAttribute(this.node, DbView.Resources.ConfigDoc.CFGATT_DATABASE, this.Database);
				DbView.Resources.ConfigDoc.Instance.AddAttribute(this.node, DbView.Resources.ConfigDoc.CFGATT_USER, this.UID);
				DbView.Resources.ConfigDoc.Instance.AddAttribute(this.node, DbView.Resources.ConfigDoc.CFGATT_PASSWORD, this.PWD);
				DbView.Resources.ConfigDoc.Instance.AddAttribute(this.node, DbView.Resources.ConfigDoc.CFGATT_DEV, this.AllowChanges);
				DbView.Resources.ConfigDoc.Instance.AddAttribute(this.node, DbView.Resources.ConfigDoc.CFGATT_ENCRYTED, this.Encrpyted);
			}
			else
			{
				if (this.node != null)
					this.node.ParentNode.RemoveChild(this.node);
			}
		}

		public void Delete()
		{
			this.isDeleted = true;
			this.RefreshNode();
		}

		public void MoveToEnd()
		{
			if (this.node == null) return; // deleted view. Ignore
			XmlNode parent = this.node.ParentNode;
			if (this.node != parent.LastChild)
			{
				parent.InsertAfter(this.node, parent.LastChild);
			}
		}

		////////////////////////////////////////////////////////////
		// TODO: Replace this with something better (but not insane)
		public static String EncryptPWD(String pwdIn)
		{
			// simple encrytion prefix begin and end with x
			return GetEncrypter().Encrpyt(pwdIn);
		}

		public static String DecryptPWD(String encryptedString)
		{
			return GetEncrypter().Decrpyt(encryptedString);
		}

		private static Encryption.SoppyCrypt GetEncrypter()
		{
			return new Encryption.SoppyCrypt("dbviewsharp", 10);
		}
	}
}
